**************************************************************************************
** title:	External Threat Environments and Individual Bias against Female Leaders **
** author:	Nam Kyu Kim and Alice Kang											 	**
** Journal:	Political Science Research and Method 					 				**
** date: 	August 2020												 				**
**************************************************************************************


cd "
use MainData, clear

log close
log using "results.txt", text replace

*********************************
*** Analyses for Main Text
*********************************

cap program drop supp
program def supp
	unique s003a if e(sample)
	estadd scalar N1=r(unique)
end


// Table 1

global controls0 "sex age married education muslim catholic religious "
global controls  "ln_gdppc e_polity2 islam_pct catholic_pct female_labor sex age married education muslim catholic religious"
global controls2 "ln_gdppc e_polity2 islam_pct catholic_pct female_labor v2lgfemleg sex age married education muslim catholic religious "
global opt "|| _all: R.year || s003a:, cov(uns) stddev nolog"

set more off
eststo clear
eststo: mixed leader_bi threat_cinc_w $controls0 $opt
	supp
eststo: mixed leader_bi threat_cinc_w $controls $opt
	supp	
eststo: mixed leader_bi threat_cinc_w $controls2 $opt
	supp
esttab, star(+ 0.10 * 0.05 ** 0.01) b(3) se(3)  label noomitted nobase transform(ln*: exp(@) exp(@)) ///
	stats(N1 N ll, fmt(0 0 0) labels("\# of countries"  "\# of respondents" "Log likelihood"))


// Table 2 

global opt "|| _all: R.year || s003a:, cov(uns) stddev nolog"

set more off
eststo clear
foreach x of varlist  midact_dum midfat_dum {
	eststo: mixed leader_bi threat_cinc_w `x' $controls $opt
	supp
}	
eststo: mixed leader_bi threat_cinc_w civilconflict $controls $opt
	supp
eststo: mixed leader_bi threat_cinc_w civilwar $controls $opt
	supp
eststo: mixed leader_bi threat_cinc_w log_tattack $controls $opt
	supp
eststo: mixed leader_bi threat_cinc_w log_nkill $controls $opt
	supp
eststo: mixed leader_bi threat_cinc_w midact_dum civilwar log_nkill $controls $opt
	supp
esttab, star(+ 0.10 * 0.05 ** 0.01) b(3) se(3)  label noomitted nobase transform(ln*: exp(@) exp(@)) ///
	stats(N1 N ll, fmt(0 0 0) labels("\# of countries"  "\# of respondents" "Log likelihood"))



// Table 3

global opt "|| _all: R.year || s003a:, cov(uns) stddev nolog"

set more off
eststo clear
eststo: mixed defense threat_cinc_w $controls  $opt 
	supp
eststo: mixed authority threat_cinc_w $controls, $opt 
	supp	
eststo: mixed leader_bi defense $controls  $opt 
	supp
eststo: mixed leader_bi authority $controls  $opt 
	supp	
eststo: mixed leader_bi conscription $controls  $opt 
	supp
eststo: mixed leader_bi lnmilper_pc lnmilex_gdp $controls  $opt 
	supp
esttab, star(+ 0.10 * 0.05 ** 0.01) b(3) se(3)  label noomitted nobase transform(ln*: exp(@) exp(@))  ///
	stats(N1 N ll, fmt(0 0 0) labels("\# of countries"  "\# of respondents" "Log likelihood"))

// Table 4

global controls0 "age married education muslim catholic religious "
global opt "|| _all: R.year || s003a:, cov(uns) stddev nolog"

eststo clear
eststo: mixed leader_bi c.threat_cinc_w##sex $controls0 $opt 
	supp
eststo: mixed leader_bi c.threat_cinc_w##sex $controls  $opt 
	supp

//  To produce estimation data for Figure 2
// ssc install parmest
	margins, dydx(threat_cinc_w) by(sex)
// 	marginsplot
	parmest, saving("post1.dta", replace) bmat(r(b)) vmat(r(V))
	qui margins, dydx(1.sex) at(threat_cinc_w=(0(.1)1))
// 	marginsplot
	parmest, saving("post2.dta", replace) bmat(r(b)) vmat(r(V))
eststo: mixed leader_bi c.threat_cinc_w##sex $controls2 $opt 
	supp
esttab, star(+ 0.10 * 0.05 ** 0.01) b(3) se(3)  label noomitted nobase transform(ln*: exp(@) exp(@))  ///
	stats(N1 N ll, fmt(0 0 0) labels("\# of countries"  "\# of respondents" "Log likelihood"))
	

	
***************************************************************
* Supporting appendix
***************************************************************

// Table A3

global controls  "ln_gdppc e_polity2 islam_pct catholic_pct female_labor sex age married education muslim catholic religious"
global opt "|| _all: R.year || s003a: if e(sample), cov(uns) stddev nolog"
	
set more off
eststo clear
qui reg leader_bi threat_cinc_w midact_dum $controls
eststo: mixed leader_bi midact_dum $controls $opt
set more off
	supp
qui reg leader_bi threat_cinc_w midfat_dum $controls
eststo: mixed leader_bi midfat_dum $controls $opt
	supp
qui reg leader_bi threat_cinc_w civilconflict $controls
eststo: mixed leader_bi civilconflict $controls $opt
	supp
qui reg leader_bi threat_cinc_w civilwar $controls
eststo: mixed leader_bi civilwar $controls $opt
	supp
qui reg leader_bi threat_cinc_w log_tattack $controls
eststo: mixed leader_bi log_tattack $controls $opt
	supp
qui reg leader_bi threat_cinc_w log_nkill $controls
eststo: mixed leader_bi log_nkill $controls $opt
	supp
esttab, star(+ 0.10 * 0.05 ** 0.01) b(3) se(3)  label noomitted nobase transform(ln*: exp(@) exp(@))  ///
	stats(N1 N ll, fmt(0 0 0) labels("\# of countries"  "\# of respondents" "Log likelihood"))

	
		
// Table A4

set more off
global controls0 "age married education muslim catholic religious "
global opt "if sex==1, || _all: R.year || s003a:, cov(uns) stddev nolog"

eststo clear
eststo: mixed leader_bi threat_cinc_w $controls0 $opt 
	supp
eststo: mixed leader_bi threat_cinc_w $controls  $opt 
	supp
eststo: mixed leader_bi threat_cinc_w $controls2 $opt 
	supp
	
global opt "if sex==0, || _all: R.year || s003a:, cov(uns) stddev nolog"

eststo: mixed leader_bi threat_cinc_w $controls0 $opt 
	supp
eststo: mixed leader_bi threat_cinc_w $controls  $opt 
	supp
eststo: mixed leader_bi threat_cinc_w $controls2 $opt 
	supp
esttab, star(+ 0.10 * 0.05 ** 0.01) b(3) se(3)  label noomitted nobase transform(ln*: exp(@) exp(@))  ///
	stats(N1 N ll, fmt(0 0 0) labels("\# of countries"  "\# of respondents" "Log likelihood"))

	


// Table A5
global opt "|| _all: R.year || s003a:, cov(uns) stddev nolog"

set more off
eststo clear
eststo: mixed obedience threat_cinc_w $controls  $opt
	supp
eststo: mixed leader_bi obedience $controls  $opt 
	supp	
	
esttab, star(+ 0.10 * 0.05 ** 0.01) b(3) se(3)  label noomitted nobase transform(ln*: exp(@) exp(@))  ///
	stats(N1 N ll, fmt(0 0 0) labels("\# of countries"  "\# of respondents" "Log likelihood"))


		
// Table A7

// ssc install reghdfe 

global fopt ", a(cowcode) cl(cowcode)"
global controls0 "sex age married education muslim catholic religious "

set more off
eststo clear
eststo: qui reghdfe leader_bi threat_cinc_w $controls0 $fopt
	supp
eststo: qui reghdfe leader_bi threat_cinc_w $controls $fopt
	supp
eststo: qui reghdfe leader_bi threat_cinc_w $controls2 $fopt
	supp


global fopt ", a(cowcode year) cl(cowcode)"
eststo: qui reghdfe leader_bi threat_cinc_w $controls0 $fopt
	supp
eststo: qui reghdfe leader_bi threat_cinc_w $controls $fopt
	supp
eststo: qui reghdfe leader_bi threat_cinc_w $controls2 $fopt
	supp

esttab, star(+ 0.10 * 0.05 ** 0.01) b(3) se(3)  label noomitted nobase ///
	stats(N1 N ll, fmt(0 0 0) labels("\# of countries"  "\# of respondents" "Log likelihood"))


// Table A8
		
global opt "|| cyear:, cov(uns) stddev nolog"

set more off
eststo clear
	eststo: mixed leader_bi threat_cinc_w $controls0 $opt
	supp
	eststo: mixed leader_bi threat_cinc_w $controls $opt
	supp	
	eststo: mixed leader_bi threat_cinc_w $controls2 $opt
	supp

global opt "|| _all: R.year || s003a: || cyear:, cov(uns) stddev nolog"
	eststo: mixed leader_bi threat_cinc_w $controls0 $opt
	supp
	eststo: mixed leader_bi threat_cinc_w $controls $opt
	supp	
	eststo: mixed leader_bi threat_cinc_w $controls2 $opt
	supp
	
esttab, star(+ 0.10 * 0.05 ** 0.01) b(3) se(3)  label noomitted nobase transform(ln*: exp(@) exp(@)) /// 
	stats(N1 N ll, fmt(0 0 0) labels("\# of countries"  "\# of respondents" "Log likelihood"))

		
// Table A9
		
global opt "|| _all: R.year || s003a:, cov(uns) stddev nolog"
		
eststo clear
	eststo: mixed leader_bi threat_w $controls0 $opt 
	supp
	eststo: mixed leader_bi threat_w $controls  $opt 
	supp
	eststo: mixed leader_bi threat_w $controls2  $opt 
	supp
	eststo: mixed leader_bi geopol $controls0 $opt 
	supp
	eststo: mixed leader_bi geopol $controls  $opt 
	supp
	eststo: mixed leader_bi geopol $controls2  $opt 
	supp
esttab, star(+ 0.10 * 0.05 ** 0.01) b(3) se(3)  label noomitted nobase transform(ln*: exp(@) exp(@)) /// 
	stats(N1 N ll, fmt(0 0 0) labels("\# of countries"  "\# of respondents" "Log likelihood"))

	
// Table A10

set more off
eststo clear
eststo: mixed leader_bi threat_cinc_w $opt
	supp
eststo: mixed leader_bi threat_cinc_w log_democracy_duration $controls $opt
	supp
eststo: mixed leader_bi threat_cinc_w lnoil_dep $controls $opt
	supp
eststo: mixed leader_bi threat_cinc_w modi  $controls $opt
	supp
eststo: mixed leader_bi threat_cinc_w mati $controls $opt
	supp
esttab, star(+ 0.10 * 0.05 ** 0.01) b(3) se(3)  label noomitted nobase transform(ln*: exp(@) exp(@)) /// 
	stats(N1 N ll, fmt(0 0 0) labels("\# of countries"  "\# of respondents" "Log likelihood"))


// Table A11
	
set more off
eststo clear
	eststo: mixed menjob threat_cinc_w $controls $opt 
	supp
	eststo: mixed menjob conscription defense $controls  $opt 
	supp
	eststo: mixed menjob defense lnmilper_pc lnmilex_gdp $controls  $opt 
	supp
	eststo: mixed mencollege threat_cinc_w $controls $opt 
	supp
	eststo: mixed mencollege conscription defense $controls $opt 
	supp
	eststo: mixed mencollege defense lnmilper_pc lnmilex_gdp $controls $opt 
	supp
esttab, star(+ 0.10 * 0.05 ** 0.01) b(3) se(3)  label noomitted nobase transform(ln*: exp(@) exp(@)) /// 
	stats(N1 N ll, fmt(0 0 0) labels("\# of countries"  "\# of respondents" "Log likelihood"))
	
// Table A12

eststo clear
eststo: mixed leader_bi threat_cinc_w $controls0 year $opt		
	supp
eststo: mixed leader_bi threat_cinc_w $controls year $opt		
	supp
eststo: mixed leader_bi threat_cinc_w $controls2 year $opt		
	supp
esttab, star(+ 0.10 * 0.05 ** 0.01) b(3) se(3)  label noomitted nobase transform(ln*: exp(@) exp(@)) /// 
	stats(N1 N ll, fmt(0 0 0) labels("\# of countries"  "\# of respondents" "Log likelihood"))


// Table A13
	
set more off		
eststo clear
	eststo: mixed leader_ord threat_cinc_w $controls0 $opt
	supp
	eststo: mixed leader_ord threat_cinc_w $controls $opt
	supp
	eststo: mixed leader_ord threat_cinc_w $controls2 $opt
	supp
	eststo: mixed leader_ord2 threat_cinc_w $controls0 $opt
	supp
	eststo: mixed leader_ord2 threat_cinc_w $controls $opt
	supp
	eststo: mixed leader_ord2 threat_cinc_w $controls2 $opt
	supp
esttab, star(+ 0.10 * 0.05 ** 0.01) b(3) se(3)  label noomitted nobase transform(ln*: exp(@) exp(@)) /// 
	stats(N1 N ll, fmt(0 0 0) labels("\# of countries"  "\# of respondents" "Log likelihood"))


// Figure A4

use MainData, clear
keep if s002 > 2
collapse (mean) threat_cinc_w ln_gdppc e_polity2 islam_pct catholic_pct female_labor v2lgfemleg (first) country, by (cowcode)
su threat_cinc_w, de
gen group = threat_cinc_w > .43

set scheme lean2, perm
qui kdensity ln_gdppc if group == 0,  addplot(kdensity ln_gdppc if group == 1, lc(ebblue)) saving(ln_gdppc.gph, replace) legend(ring(0) pos(11)  size(vsmall) label(1 "Low threat") label(2 "High threat")) xtitle("GDP per capita (log)", size(small)) ytitle("Density", size(small)) title("") note("") 

qui  kdensity e_polity2 if group == 0, addplot(kdensity e_polity2 if group == 1, lc(ebblue)) saving(e_polity2.gph, replace) legend(ring(0) pos(11)  size(vsmall) label(1 "Low threat") label(2 "High threat")) xtitle("Polity score", size(small)) ytitle("Density", size(small)) title("") note("")

qui kdensity female_labor if group == 0, addplot(kdensity female_labor if group == 1, lc(ebblue)) saving(female_labor.gph, replace) legend(ring(0)  size(vsmall) pos(11) label(1 "Low threat") label(2 "High threat")) xtitle("Female employment (%)", size(small)) ytitle("Density", size(small)) title("") note("")

qui kdensity v2lgfemleg if group == 0, addplot(kdensity v2lgfemleg if group == 1, lc(ebblue)) saving(v2lgfemleg.gph, replace) legend(ring(0) pos(2)  size(vsmall) label(1 "Low threat") label(2 "High threat")) xtitle("Female legislative representation", size(small)) ytitle("Density", size(small)) title("") note("")

graph combine ln_gdppc.gph e_polity2.gph female_labor.gph v2lgfemleg.gph, col(2) xsize(6)

// Table A1

use MainData, clear

global controls2 "ln_gdppc e_polity2 islam_pct catholic_pct female_labor v2lgfemleg sex age married education muslim catholic religious "
reg leader_bi threat_cinc_w  $controls2
sutex2 leader_bi leader_ord leader_ord2 threat_cinc_w geopol  midact3_dum midhi3_dum midfat3_dum $controls2 v2lgfemleg  if e(sample),  perc(50) minmax dig(2) varlab na(na) saving(summary.tex) replace

// Table A6

use "Supp.dta", clear

cap program drop supp2
program def supp2
	unique cowcode if e(sample)
	estadd scalar N1=r(unique)
	unique year if e(sample)
	estadd scalar N2=r(unique)
end

xtset cowcode year

eststo clear
eststo: qui logit f.conscription threat_cinc_w ln_gdppc e_polity2 ucdp_cvconflict ucdp_intconflict lnpop, cl(cowcode)
	supp2
eststo: qui xtlogit f.conscription threat_cinc_w ln_gdppc e_polity2 ucdp_cvconflict ucdp_intconflict lnpop, fe
	supp2
eststo: qui reg f.lnmilper_pc threat_cinc_w ln_gdppc e_polity2 ucdp_cvconflict ucdp_intconflict lnpop lnmilper_pc,  cl(cowcode)
	supp2
eststo: qui xtreg f.lnmilper_pc threat_cinc_w ln_gdppc e_polity2 ucdp_cvconflict ucdp_intconflict lnpop lnmilper_pc, fe cl(cowcode)
	supp2
eststo: qui reg f.lnmilex_gdp threat_cinc_w ln_gdppc e_polity2 ucdp_cvconflict ucdp_intconflict lnpop lnmilex_gdp, cl(cowcode)
	supp2
eststo: qui xtreg f.lnmilex_gdp threat_cinc_w ln_gdppc e_polity2 ucdp_cvconflict ucdp_intconflict lnpop lnmilex_gdp, fe cl(cowcode)
	supp2

esttab, star(+ 0.10 * 0.05 ** 0.01) b(3) se(3)  label noomitted nobase


